package com.ygswglxt.notification.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ygswglxt.common.Result;
import com.ygswglxt.notification.entity.Notification;
import com.ygswglxt.notification.mapper.NotificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NotificationService {
    
    @Autowired
    private NotificationMapper notificationMapper;
    
    // 创建通知
    public void createNotification(Long userId, String type, String title, String content, Long relatedId, String relatedType) {
        try {
            System.out.println("========== 开始创建通知 ==========");
            System.out.println("userId: " + userId + ", type: " + type + ", title: " + title);
            Notification notification = new Notification();
            notification.setUserId(userId);
            notification.setType(type);
            notification.setTitle(title);
            notification.setContent(content);
            notification.setRelatedId(relatedId);
            notification.setRelatedType(relatedType);
            notification.setIsRead(false);
            notification.setCreateTime(new Date());
            int result = notificationMapper.insert(notification);
            if (result > 0) {
                System.out.println("✓ 通知创建成功 - id: " + notification.getId());
            } else {
                System.err.println("❌ 通知创建失败 - 插入返回0");
            }
            System.out.println("========== 通知创建完成 ==========");
        } catch (Exception e) {
            System.err.println("❌ 创建通知异常: " + e.getMessage());
            e.printStackTrace();
            throw e; // 重新抛出异常，让调用者知道失败
        }
    }
    
    // 获取用户的通知列表
    public Result<List<Notification>> getUserNotifications(Long userId, Integer page, Integer size) {
        QueryWrapper<Notification> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("create_time");
        
        if (page != null && size != null) {
            int offset = (page - 1) * size;
            wrapper.last("LIMIT " + offset + ", " + size);
        }
        
        List<Notification> notifications = notificationMapper.selectList(wrapper);
        return Result.success(notifications);
    }
    
    // 获取未读通知数量
    public Result<Map<String, Object>> getUnreadCount(Long userId) {
        QueryWrapper<Notification> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("is_read", false);
        Long count = notificationMapper.selectCount(wrapper);
        
        Map<String, Object> result = new HashMap<>();
        result.put("unreadCount", count);
        return Result.success(result);
    }
    
    // 标记为已读
    public Result<String> markAsRead(Long notificationId, Long userId) {
        Notification notification = notificationMapper.selectById(notificationId);
        if (notification == null) {
            return Result.error("通知不存在");
        }
        if (!notification.getUserId().equals(userId)) {
            return Result.error("无权限操作");
        }
        
        notificationMapper.markAsRead(notificationId);
        return Result.success("标记成功");
    }
    
    // 全部标记为已读
    public Result<String> markAllAsRead(Long userId) {
        notificationMapper.markAllAsRead(userId);
        return Result.success("全部标记成功");
    }
    
    // 删除通知
    public Result<String> deleteNotification(Long notificationId, Long userId) {
        Notification notification = notificationMapper.selectById(notificationId);
        if (notification == null) {
            return Result.error("通知不存在");
        }
        if (!notification.getUserId().equals(userId)) {
            return Result.error("无权限操作");
        }
        
        notificationMapper.deleteById(notificationId);
        return Result.success("删除成功");
    }
}

